
#include <sys/types.h>          
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>

/*socket
 *bind
 *send/recv
 */

#define SERVER_PORT 8888
#define BACKLOG 10

 int main(int argc,char **argv){
    int iSocketServer;
    int iRet;
    int iAddrlen;
    int iSocketClient;
    struct sockaddr_in tSocketServerAddr;
    struct sockaddr_in tSocketClientAddr;

    int iRecvLen;
    unsigned char ucRecvBuf[1000];
    int iClientNum = -1;

    iSocketServer = socket(AF_INET, SOCK_DGRAM, 0);
    if(iSocketServer == -1){
        printf("socket error!\n");
        return -1;
    }

    tSocketServerAddr.sin_family = AF_INET;
    tSocketServerAddr.sin_port = htons(SERVER_PORT);
    tSocketServerAddr.sin_addr.s_addr = INADDR_ANY;
    memset(tSocketServerAddr.sin_zero, 0, 8);
    iRet = bind(iSocketServer, (const struct sockaddr *)&tSocketServerAddr,
                sizeof(struct sockaddr));
    if(iRet == -1){
        printf("bind error!\n");
        return -1;
    }


    while(1){
        iAddrlen = sizeof(struct sockaddr_in);
        iRecvLen = recvfrom(iSocketServer,ucRecvBuf,999,0,(struct sockaddr *)&tSocketClientAddr,&iAddrlen);
        if(iRecvLen >0){
            ucRecvBuf[iRecvLen] = '\0';
            printf("Get Msg From %s : %s\n",inet_ntoa(tSocketClientAddr.sin_addr),ucRecvBuf);
        }
    }
    close(iSocketServer);

    return 0;
 }